<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <script type="text/javascript" src="jquery/jquery-3.4.1.min.js"></script>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/signin.css" rel="stylesheet">
</head>
<body>
<!--下半部分-->
<div class="from-signin">
    <h3>修改个人资料</h3>
    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="login_acct">你的账号</label>
            <span id="mes" style="font-size: 12px;color: red;"></span>
            <input type="text"  class="form-control" id="login_acct">
        </div>
        <div class="form-group col-md-6">
            <label for="user_pswd">修改密码</label>
            <span id="mesTwo" style="font-size: 12px;color: red;"></span>
            <input type="password" name="user_pswd" class="form-control" id="user_pswd">
        </div>
    </div>

    <div class="form-group">
        <label for="pswdConfirm">确认修改密码</label>
        <input type="password" name="user_name" class="form-control" id="pswdConfirm" >
    </div>

    <div class="form-group">
        <label for="email">修改email</label>
        <input type="text" name="email" class="form-control" id="email" >
    </div>
    <div class="form-group">
        <label class="red hidden col-md-9" id="errorLog"></label>
    </div>
    <div class="row">
        <div class="col-lg-6 offset-3">
            <button class="btn btn-lg btn-primary btn-block " id="registerBtn">修改</button>
        </div>
        <div class="col-lg-6 offset-5">
            <button class="btn btn-link pull-right" id="returnRegister">
                主页
            </button>
        </div>
    </div>

</div>

<script type="text/javascript">
    $(document).ready(function () {
        var userNameSuccessful = null;
        var passwordSuccessful = null;
        $("#returnRegister").click(function () {
            $(location).attr("href","login")
        });
        $("#login_acct").blur(function () {
            var strLogin = $("#login_acct").val();
            if (strLogin.trim()===""){
                $("#mes").text("用户名不能为空");
            }else {
                var unRag = /^[a-zA-Z][a-zA-Z0-9_]{6,14}$/;
                var ok = unRag.test(strLogin);
                if (ok){
                    userNameSuccessful = true;
                }else {
                    $("#mes").text("用户名格式不对");
                }
            }
        });
        $("#login_acct").focus(function () {

            if ($("#mes").text() != null){
                $("#login_acct").val("");
            }
            $("#mes").text("")

        });
        $("#pswdConfirm").blur(function () {
            var pwd = $("#user_pswd").val();
            var pwd2 = $("#pswdConfirm").val();
            if (pwd.trim()===""){
                $("#mesTwo").text("密码不能为空");
            }else {
                if (pwd === pwd2){
                    $("#mesTwo").text("验证成功");
                    passwordSuccessful = true;
                }else {
                    $("#mesTwo").text("前后两次密码不一致");
                }
            }
        });
        $("#registerBtn").click(function () {
            if (userNameSuccessful && passwordSuccessful) {
                $.ajax({
                    url: "user/updateInfo",
                    type: "post",
                    dataType: "json",
                    data: {
                        loginAcct: $("#login_acct").val(),
                        userPswd: $("#user_pswd").val(),
                        email: $("#email").val()
                    },
                    success: function (result) {
                            var flag = result.errorCode;
                            if (flag){
                                alert("修改成功");
                                $(location).attr("href","login");
                            }else {
                                alert("修改失败")
                            }
                    }

                })
            }else {
                alert("您输入的信息有误，请重新核对信息")
            }
        });

    });
</script>
</body>
</html>